package top.ztf.erp.service.imp;

import org.apache.ibatis.session.SqlSession;
import top.ztf.erp.dao.PermissionDao;
import top.ztf.erp.dao.RolePermissionRelationDao;
import top.ztf.erp.domain.Permission;
import top.ztf.erp.domain.PermissionNode;
import top.ztf.erp.service.PermissionService;
import top.ztf.erp.utils.MyBatisUtil;
import top.ztf.erp.visitors.CheckVisitorIml;
import top.ztf.erp.visitors.DeleteVisitorIml;

import java.util.List;
import java.util.Optional;

public class PermissionServiceImp implements PermissionService {

    /**
     * 返回所有权限的的list
     * @return
     */
    @Override
    public List<Permission> getPermissionAll() {
        SqlSession session = MyBatisUtil.getSqlSession();
        PermissionDao permissionDao = session.getMapper(PermissionDao.class);
        return permissionDao.selectAll();
    }

    /**
     * 根据id查询权限菜单
     * @param id
     * @return
     */
    @Override
    public Permission getPermissionById(String id) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        PermissionDao permissionDao = sqlSession.getMapper(PermissionDao.class);
        return permissionDao.selectPermissionById(id);
    }

    /**
     * 添加权限菜单
     * @param permission
     * @return
     */
    @Override
    public int addPermission(Permission permission) {
        SqlSession sqlSession  = null;
        try {
            sqlSession = MyBatisUtil.getSqlSession();
            PermissionDao permissionDao = sqlSession.getMapper(PermissionDao.class);
            int count = permissionDao.addPermission(permission);
            sqlSession.commit();
            return 1;
        }catch (Exception e){
            Optional.ofNullable(sqlSession).ifPresent(SqlSession::rollback);
            e.printStackTrace();
            return 0;
        }
    }

    /**
     * 根据id修改权限菜单
     * @param permission
     * @return
     */
    @Override
    public int updatePermissionById(Permission permission) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MyBatisUtil.getSqlSession();
            PermissionDao permissionDao = sqlSession.getMapper(PermissionDao.class);
            int i = permissionDao.updatePermissionById(permission);
            sqlSession.commit();
            return 1;
        }catch (Exception e){
            Optional.ofNullable(sqlSession).ifPresent(SqlSession::rollback);
            e.printStackTrace();
            return 0;
        }
    }

    /**
     * 根据id删除权限菜单 以及之后的子菜单
     * 还要每一个菜单它是否被占用
     * @param id
     * @return
     */
    @Override
    public int deletePermissionById(String id) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        PermissionDao permissionDao = sqlSession.getMapper(PermissionDao.class);
        RolePermissionRelationDao relationDao = sqlSession.getMapper(RolePermissionRelationDao.class);

        Permission permission = permissionDao.selectPermissionById(id);
        PermissionNode clone = permission.clone(PermissionNode.class);

        CheckVisitorIml visitor = new CheckVisitorIml(permissionDao, relationDao);
        visitor.visitCheckPermissionNode(clone);

        try {
            if (!visitor.getStatus()){
                //没有被占用
                DeleteVisitorIml deleteVisitorIml = new DeleteVisitorIml(permissionDao,sqlSession);
                deleteVisitorIml.doCHeckPermissionNode(clone);
                //最终,我们提交事务,必须到业务层提交,这样才能保证,每一条数据是删除是合法的,否则不删除
                sqlSession.commit();
            }
        }catch (Exception e){
            sqlSession.rollback();
            e.printStackTrace();
            return 0;
        }
        return 1;
    }



    /**
     * 根据名字查询权限菜单
     * @param name
     * true 表示不存在
     * false 表示存在
     * @return
     */
    @Override
    public boolean verifyPermissionName(String name,String id) {
        SqlSession sqlSession = null;
        sqlSession = MyBatisUtil.getSqlSession();
        PermissionDao permissionDao = sqlSession.getMapper(PermissionDao.class);
        Permission permission = permissionDao.selectPermissionByName(name,id);
        if (permission==null){
            //不存在
            return true;
        }else {
            //存在
            return false;
        }
    }

    /**
     * 获取PermissionNode 集合对象
     * @return
     */
    @Override
    public List<PermissionNode> getPermissionAllNode() {
        SqlSession session = MyBatisUtil.getSqlSession();
        PermissionDao permissionDao = session.getMapper(PermissionDao.class);
        return permissionDao.selectAllNode();
    }


}
